package com.itfreer.gis.tansform;

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.processing.Operations;
import org.geotools.data.DataSourceException;
import org.geotools.factory.Hints;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.referencing.ReferencingFactoryFinder;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import java.io.File;
import java.io.IOException;

/**
 * 基于geotools coverage的影像坐标系的转换
 */
public class TifSrcTransform {

	/**
	 * jai_core ,jai_imageio必须添加不然编译会报错(gradle已配置)
	 *
	 */
	public static void main(String[] args) {

		File file = new File("D:/shp/tif/LGQ_18.tif");

		GeoTiffReader reader = null;
		try {
			reader = new GeoTiffReader(file);
		} catch (DataSourceException e) {
			e.printStackTrace();
		}
		GridCoverage2D old2D = null;
		try {
			old2D = reader.read(null);
		} catch (IOException e) {
			e.printStackTrace();
		}

		Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
		CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
		CoordinateReferenceSystem crs = null;
		try {
			crs = factory.createCoordinateReferenceSystem("EPSG:4326");
		} catch (FactoryException e) {
			e.printStackTrace();
		}
		Operations.DEFAULT.resample(old2D, crs);
	}

}
